package zjs.java.demo;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.graphics.Color;

import zjs.java.AreaSystem;
import zjs.java.AreaSystem.Area;
import zjs.java.AreaSystem.Area.Brush;
import zjs.java.CursorChangeEvent;
import zjs.java.CursorClickEvent;
import zjs.java.CursorMoveEvent;
import zjs.java.IArea;
import zjs.java.Region;
import zjs.java.Remover;
import zjs.java.Response;

public class BasicDemo {
	static int[] createCircle( int radius, int centerX, int centerY ) {
		int[] points = new int[360 * 2];
		for ( int i = 0; i < 360; i++ ) {
			points[i * 2] = centerX + ( int ) ( radius * Math.cos( Math.toRadians( i ) ) );
			points[i * 2 + 1] = centerY + ( int ) ( radius * Math.sin( Math.toRadians( i ) ) );
		}
		return points;
	}

	static byte[] getRegion( int width, int height, Region region ) {
		byte[] retVal = new byte[width * height];
		for ( int y = 0; y != height; ++y ) {
			for ( int x = 0; x != width; ++x ) {
				if ( region.contains( x, y ) ) {
					retVal[y * width + x] = -1;
				}
				else {
					retVal[y * width + x] = 0;
				}
			}
		}
		return retVal;
	}

	/**
	 * @param args
	 */
	public static void main( String[] args ) {
		final Display display = Display.getDefault();
		final Shell shell = new Shell( display, SWT.SHELL_TRIM );
		shell.open();

		// 创建一个区域系统
		final AreaSystem as = AreaSystem.newAreaSystem( shell );

		// 设置它的位置,大小和范围
		as.setX( 100 );
		as.setY( 100 );
		as.resize( 500, 500, getRegion( 500, 500, new Region() {
			@Override
			public boolean contains( int x, int y ) {
				return ( x - 250 ) * ( x - 250 ) + ( y - 250 ) * ( y - 250 ) <= 250 * 250;
			}
		} ) ); // 圆形区域

		// 创建一个画布,并绘制一个矩形,会因为圆形范围的原因被截取为圆形
		Brush gc = as.newCanvas( 500, 500 );
		gc.setBackground( display.getSystemColor( SWT.COLOR_BLUE ) );
		gc.fillRectangle( 0, 0, 500, 500 );

		as.onCursorOut( new Response<CursorChangeEvent>() {
			@Override
			public void trig( CursorChangeEvent eventInfo ) {
				System.out.println( "Out" );
			}
		} );

		// 可拖动的矩形
		class DraggableRectangle {
			Area area;
			Brush gc;
			Color backgroundColor;
			Color hoverColor;
			int width;
			int height;
			int r;
			Remover cursorMove;
			Remover cursorUp;
			Remover cursorOut;

			public DraggableRectangle( IArea parentArea, int x, int y, int aWidth, int aHeight, Color aBackgroundColor, Color aHoverColor ) {
				// 设置字段
				backgroundColor = aBackgroundColor;
				hoverColor = aHoverColor;
				width = aWidth;
				height = aHeight;
				r = width / 2;

				// 建立新区域,并设置位置和尺寸
				area = parentArea.area();
				area.setX( x );
				area.setY( y );
				area.resize( width, height, getRegion( width, height, new Region() {
					@Override
					public boolean contains( int x, int y ) {
						return ( x - r ) * ( x - r ) + ( y - r ) * ( y - r ) <= r * r;
					}
				} ) );

				// 建立画布并绘制背景
				gc = area.newCanvas( width, height );
				gc.setBackground( backgroundColor );
				gc.fillRectangle( 0, 0, width, height );

				// cursorDown事件
				area.onCursorDown( new Response<CursorClickEvent>() {
					@Override
					public void trig( CursorClickEvent eventInfo ) {
						// 记录点击时的光标在区域中的位置
						final int areaX = eventInfo.x, areaY = eventInfo.y;

						cursorMove = as.onCursorMove( new Response<CursorMoveEvent>() {
							@Override
							public void trig( CursorMoveEvent eventInfo ) {
								area.setX( eventInfo.x - areaX );
								area.setY( eventInfo.y - areaY );
								as.redraw();
							}
						} );

						cursorUp = as.onCursorUp( new Response<CursorClickEvent>() {
							@Override
							public void trig( CursorClickEvent eventInfo ) {
								cursorMove.remove();
								cursorUp.remove();
								cursorOut.remove();
							}
						} );

						cursorOut = as.onCursorOut( new Response<CursorChangeEvent>() {
							@Override
							public void trig( CursorChangeEvent eventInfo ) {
								cursorMove.remove();
								cursorUp.remove();
								cursorOut.remove();
								System.out.println( "out" );
							}
						} );
					}
				} );

				// 悬停高亮
				area.onCursorIn( new Response<CursorChangeEvent>() {
					@Override
					public void trig( CursorChangeEvent eventInfo ) {
						// 置顶
						area.insertUpper( area.getTopArea() );
						gc.setBackground( hoverColor );
						gc.fillRectangle( 0, 0, width, height );
						as.redraw();
						System.out.println( "In" );
					}
				} );

				// 切出时变回一般亮度
				area.onCursorOut( new Response<CursorChangeEvent>() {
					@Override
					public void trig( CursorChangeEvent eventInfo ) {
						gc.setBackground( backgroundColor );
						gc.fillRectangle( 0, 0, width, height );
						as.redraw();
					}
				} );
			}
		}

		new DraggableRectangle( as, 100, 100, 100, 100, display.getSystemColor( SWT.COLOR_RED ), display.getSystemColor( SWT.COLOR_DARK_GREEN ) );
		new DraggableRectangle( as, 150, 150, 200, 200, display.getSystemColor( SWT.COLOR_YELLOW ), display.getSystemColor( SWT.COLOR_DARK_MAGENTA ) );

		// 重绘
		as.redraw();

		while ( !shell.isDisposed() ) {
			if ( !display.readAndDispatch() ) display.sleep();
		}
		display.dispose();
	}
}
